using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using WP.Modules;
using WP.Commons;
using WP.Controls;
using WP.Controls.GridColumn;
using WP.DB;
using System.Data.SqlClient;


namespace WP.Modules
{
    public partial class frmGetFitPostCondition : System.Windows.Forms.Form
    {

        public frmGetFitPostCondition()
        {
            InitializeComponent();
        }

        #region Var
        private string mstrCondition;
        private string Condition
        {
            get { return mstrCondition; }
            set { mstrCondition = value; }
        }

        public enum FromModuleType{Reman=0,InvTagTrf,ProductionIssues,BackFlashing,InvCostAdj};
        private FromModuleType mFromModule;
        private FromModuleType FromModule
        {
            get { return mFromModule; }
            set { mFromModule = value; }
        }
        #endregion Var

        public string GetFitPostCondition(string strCondition)
        {
            return GetFitPostCondition(strCondition, 0);
        }

        public string GetFitPostCondition(string strCondition, FromModuleType frommodule)
        {
            bool blnChangCost = false;
            this.Condition=strCondition ;
            this.FromModule = frommodule;                        

            switch(this.FromModule)
            {
                case FromModuleType.Reman :         
                    this.Text  = "Finished Work Order Post Query";
                    break;
                case FromModuleType.InvTagTrf:
                    this.Text = "Tag Transfer Post Query";
                    break;
                case FromModuleType.ProductionIssues :
                    this.Text  = "Production Issues Post Query";
                    break;
                case FromModuleType.BackFlashing :
                    this.Text  = "Production Backflashing Post Query";
                    break;
                case FromModuleType.InvCostAdj:
                    this.Text  = "Inventory Cost Adjustment Post Query";
                    break;
            }

            if (!FillListPost( ref blnChangCost)) return "";

            if (blnChangCost)
                this.ShowDialog();
            else
                btnOK_Click(null,null);
            

            return this.Condition;
        }

        private bool FillListPost(  ref bool blnChangCost)
        {
            DBAccess db=null;
            string strSql="";
            double dblTotQty=0,sngQty=0,sngPL=0;
            DataTable dtHdr=null,dtPL=null; 
            DataRow drPL=null;

            if(this.FromModule == FromModuleType.Reman )
            {
                #region Reman
                blnChangCost = false;
                
                db=new DBAccess(false,false);

                double sngMax =CvtFunc.ToDouble(DataFunctions.GetmMiscValue(db,"WtAvgCostChangedMax", "RM"));
                double sngMin =CvtFunc.ToDouble(DataFunctions.GetmMiscValue(db,"WtAvgCostChangedMin", "RM"));
                
                InitHead();
                
                strSql = "select * from rmwkorderhdr where " +this.Condition+ " order by ordno";
                dtHdr =db.GetDataTable(strSql);
                if(dtHdr==null||dtHdr.Rows.Count ==0)
                {
                    dtHdr=null;
                    db.CloseCon();
                    return false;
                }
                
                DataFunctions.DelTemporyTable(db, "#TmpImInvLoc");
                strSql =string .Format("create table #TmpImInvLoc ({0} \r\n {1} \r\n {2} \r\n {3} \r\n {4} \r\n {5} \r\n {6} \r\n {7} \r\n {8} \r\n {9}) "
                    ,"RecId int NOT NULL IDENTITY (1, 1)"
                    ," ,OrdNo char(8) Null"
                    ," ,SeqNo int Null "
                    ," ,ItemNo char(20) null"
                    ," ,LocationNo Char(6) null"
                    ," ,CostUnit Char(5) null"
                    ," ,WtAvgCost decimal(18,6) null"
                    ," ,Qty decimal(18,6) null"
                    ," ,Unit Char(5) null"
                    ," ,Grouping bit Null "                
                    );
                db.ExecuteSql(strSql);
                
                
                foreach(DataRow drHdr in dtHdr.Rows)
                {
                    #region get WtAvgCost
                    strSql =string.Format("{0} \r\n {1} \r\n {2}{3}{4}"
                        , "insert into #TmpImInvLoc(OrdNo,SeqNo,ItemNo,LocationNo,WtAvgCost,CostUnit,Grouping,Qty,Unit)"
                        ," select a.OrdNo,a.SeqNo,b.ItemNo,b.LocationNo,b.WtAvgCost,a.CostUnit,c.Grouping,a.Qty,a.Unit"
                        ," from rmwkorderinput a inner join iminvloc b on a.itemno=b.itemno and a.locno=b.locationno inner join iminvmaster c on a.itemno=c.itemno where  a.ordno='" 
                        ,drHdr["OrdNo"]
                        ,"'"
                        );
                    db.ExecuteSql(strSql);
                    
                    strSql =string.Format ( "select * from #TmpImInvLoc where ordno='{0}'",drHdr["OrdNo"]);
                    DataTable dtTmpInvLoc=db.GetDataTable(strSql);
                    foreach(DataRow drTmpInvLoc in dtTmpInvLoc.Rows)
                    {                     
                        if(CvtFunc.ToBoolean(  drTmpInvLoc["Grouping"]))
                        {
                            if(CvtFunc.ToDouble (  drTmpInvLoc["Qty"])!= 0 )
                            {
                                dblTotQty =DataFunctions.GetRMGroupingTotCost(db,CvtFunc.ToString(drTmpInvLoc["OrdNo"]),CvtFunc.ToLong( drTmpInvLoc["SeqNo"]));
                                        
                                sngQty =DataFunctions.UnitCnv(db ,CvtFunc.ToString (drTmpInvLoc["ItemNo"]),CvtFunc.ToDouble (drTmpInvLoc["Qty"]),CvtFunc.ToString (drTmpInvLoc["Unit"]),CvtFunc.ToString (drTmpInvLoc["CostUnit"]));
                                
                                if (sngQty!=0)
                                    sngQty = dblTotQty / sngQty;
                                else
                                    sngQty=0;

                                strSql = string.Format("update #TmpImInvLoc set WtAvgCost={0} where recid={1}", CvtFunc.ToDouble(sngQty, DataFunctions.ItemScale(db, ScaleType.gCostScale, CvtFunc.ToString(drTmpInvLoc["ItemNo"]))), CvtFunc.ToLong(drTmpInvLoc["RecId"]));
                                db.ExecuteSql (strSql);     
                                
                            }
                        }
                        else
                        {
                            if( DataFunctions.UseActualCost(db,CvtFunc.ToString (drTmpInvLoc["ItemNo"])))
                            {
                                sngQty = DataFunctions.GetTaglotActualCost(db,CvtFunc.ToString (drTmpInvLoc["OrdNo"]),CvtFunc.ToLong( drTmpInvLoc["SeqNo"]), CvtFunc.ToString (drTmpInvLoc["CostUnit"]),true,GetTaglotActualCostType.RemanIn );

                                strSql = string.Format("update #TmpImInvLoc set WtAvgCost={0} where recid={1}", sngQty, CvtFunc.ToLong(drTmpInvLoc["RecId"]));
                                db.ExecuteSql (strSql);     
                            }
                        }                        
                    }
                    #endregion get WtAvgCost
                    
                    ListViewItem lst=new ListViewItem("");
                    lst.Checked =true;
                    lst.SubItems.Add(CvtFunc.ToString(drHdr["OrdNo"]));
                    strSql = "select sum(case when isnull(a.cost,0)=0 then (b.WtAvgCost-a.cost)*a.totcost else (b.WtAvgCost-a.cost)*a.totcost/a.cost end ) as totcost  from rmwkorderinput a inner join #TmpImInvLoc b on a.itemno=b.itemno and a.locno=b.locationno  and a.Ordno=b.Ordno and a.Seqno=b.Seqno inner join iminvmaster c on a.itemno=c.itemno where c.stocking=1 and a.cost<>b.WtAvgCost and a.ordno='" + CvtFunc.ToString(drHdr["OrdNo"]) + "'";
                    sngPL=db.GetDoubleValue(strSql);
               
                    if( sngPL != 0 )
                    {
                        sngPL =CvtFunc.ToDouble( drHdr["ProfitLoss"]) - sngPL;
                        lst.SubItems.Add("Stocking only Cost Changed. Total Out Of Balance by " + BaseFunctions.FormatNumeric(sngPL, "NS.2"));
                        lst.SubItems.Add(sngPL.ToString ());
                        if( sngPL < sngMin || sngPL > sngMax )
                        {
                            lst.Checked = false;
                            blnChangCost = true;
                        }
                        lst.Tag = "PL";
                    }
                    else
                    {
                        lst.SubItems.Add("Cost Not Changed.");
                    }

                    LstPost.Items.Add (lst);
                }
                 
                #endregion Reman
                return true;

            }
            else if (FromModule==FromModuleType.BackFlashing )
            {
                #region BackFlashing
                //FillListPost = False
                //blnChangCost = False
                //sngMax = GetmMiscValue("WtAvgCostChangedMax", "RM")
                //sngMin = GetmMiscValue("WtAvgCostChangedMin", "RM")
                
                //InitHead
                
                //strSQl = "select * from rmBackFlashinghdr where " & strCondition & " order by ordno"
                //Set rsHdr = New ADODB.Recordset
                //Set rsPl = New ADODB.Recordset
                
                //rsHdr.Open strSQl, gcn, adOpenKeyset, adLockPessimistic
                //If rsHdr.RecordCount = 0 Then
                //    FillListPost = False
                //    Release_RS rsHdr
                //    Exit Function
                //End If
                
                //DelTemporyTable "#TmpImInvLoc", gcn
                //strSQl = "create table #TmpImInvLoc (RecId int NOT NULL IDENTITY (1, 1)"
                //strSQl = strSQl & vbCrLf & " ,OrdNo char(8) Null"
                //strSQl = strSQl & vbCrLf & " ,SeqNo int Null "
                //strSQl = strSQl & vbCrLf & " ,ItemNo char(20) null"
                //strSQl = strSQl & vbCrLf & " ,LocationNo Char(6) null"
                //strSQl = strSQl & vbCrLf & " ,CostUnit Char(5) null"
                //strSQl = strSQl & vbCrLf & " ,WtAvgCost decimal(18,6) null"
                //strSQl = strSQl & vbCrLf & " )"
                //gcn.Execute strSQl
                
                //While Not rsHdr.EOF
                //    strSQl = "insert into #TmpImInvLoc(OrdNo,SeqNo,ItemNo,LocationNo,WtAvgCost,CostUnit)"
                //    strSQl = strSQl & vbCrLf & " select a.OrdNo,a.SeqNo,b.ItemNo,b.LocationNo,b.WtAvgCost,a.CostUnit"
                //    strSQl = strSQl & vbCrLf & " from rmBackFlashinginput a inner join iminvloc b on a.itemno=b.itemno and a.locno=b.locationno inner join iminvmaster c on a.itemno=c.itemno where  a.ordno='" & rsHdr!OrdNo & "'"
                //    gcn.Execute strSQl
                    
                //    strSQl = "select * from #TmpImInvLoc where ordno='" & rsHdr!OrdNo & "'"
                //    Set rsTmpInvLoc = New ADODB.Recordset
                //    rsTmpInvLoc.Open strSQl, gcn, adOpenKeyset, adLockOptimistic
                //    While Not rsTmpInvLoc.EOF
                //        If mblnUseFIFO Then
                //            Call GetFIFOCost("RMI", rsTmpInvLoc!OrdNo, rsTmpInvLoc!SeqNo, dblFIFOCost, , , , "BackFlash")
                //            strSQl = "select costunit from iminvmaster where itemno='" & rsTmpInvLoc!ItemNo & "' and costunit<>'" & rsTmpInvLoc!CostUnit & "'"
                //            Set rsItem = gcn.Execute(strSQl)
                //            If rsItem.EOF Then
                //                rsTmpInvLoc!WtAvgCost = FormatNumeric(dblFIFOCost, "n." & ItemScale(gCostScale, rsTmpInvLoc!ItemNo))
                //            Else
                //                Call UnitCnv(rsTmpInvLoc!ItemNo, dblFIFOCost, rsTmpInvLoc!CostUnit, rsItem!CostUnit, rsTmpInvLoc!WtAvgCost)
                //                rsTmpInvLoc!WtAvgCost = FormatNumeric(rsTmpInvLoc!WtAvgCost, "n." & ItemScale(gCostScale, rsTmpInvLoc!ItemNo))
                //            End If
                //            rsTmpInvLoc.Update
                //        Else
                //            If UseActualCost(rsTmpInvLoc!ItemNo) Then
                //                rsTmpInvLoc!WtAvgCost = GetTaglotActualCost(rsTmpInvLoc!OrdNo, rsTmpInvLoc!SeqNo, rsTmpInvLoc!CostUnit, , 5)
                //                rsTmpInvLoc.Update
                //            End If
                //        End If
                        
                //        rsTmpInvLoc.MoveNext
                //    Wend
                    
                    
                    
                //    Set lst = LstPost.ListItems.Add(, , "")
                //    lst.ListSubItems.Add , , rsHdr!OrdNo
                //    lst.Checked = True
                //    strSQl = "select sum(case when isnull(a.cost,0)=0 then (b.WtAvgCost-a.cost)*a.totcost else (b.WtAvgCost-a.cost)*a.totcost/a.cost end )  from rmBackFlashinginput a inner join #TmpImInvLoc b on a.itemno=b.itemno and a.locno=b.locationno  and a.Ordno=b.Ordno and a.Seqno=b.Seqno inner join iminvmaster c on a.itemno=c.itemno where c.stocking=1 and a.cost<>b.WtAvgCost and a.ordno='" & rsHdr!OrdNo & "'"
                //    If rsPl.State = adStateOpen Then rsPl.Close
                //    rsPl.Open strSQl, gcn, adOpenForwardOnly, adLockReadOnly
                    
                    
                //    sngPL = Val("" & rsPl.Fields(0))
                //    If sngPL <> 0 Then
                //        sngPL = Val(FormatNumeric((rsHdr!ProfitLoss - sngPL), "n.2"))
                //        lst.ListSubItems.Add , , "Stocking only Cost Changed. Total Out Of Balance by " & FormatNumeric(sngPL, "NS.2")
                //        lst.ListSubItems.Add , , sngPL
                //        If sngPL < sngMin Or sngPL > sngMax Then
                //            lst.Checked = False
                //            blnChangCost = True
                //        End If
                //        lst.Tag = "PL"
                //    Else
                //        lst.ListSubItems.Add , , "Cost Not Changed."
                //    End If
                //    rsHdr.MoveNext
                //Wend
                 
                
                //Release_RS rsHdr

                #endregion BackFlashing
                return true;
            }
            else if (this.FromModule==FromModuleType.ProductionIssues )
            {
                #region ProductionIssues
            //    FillListPost = False
            //    blnChangCost = False
                
            //    InitHead
                
            //    strSQl = "select * from iminvProdIssuesHdr where " & strCondition & " order by ordno"
            //    Set rsHdr = New ADODB.Recordset
            //    Set rsPl = New ADODB.Recordset
                
            //    rsHdr.Open strSQl, gcn, adOpenKeyset, adLockPessimistic
            //    If rsHdr.RecordCount = 0 Then
            //        FillListPost = False
            //        Release_RS rsHdr
            //        Exit Function
            //    End If
                
            //    DelTemporyTable "#TmpImInvLoc", gcn
            //    strSQl = "create table #TmpImInvLoc (RecId int NOT NULL IDENTITY (1, 1)"
            //    strSQl = strSQl & vbCrLf & " ,OrdNo char(8) Null"
            //    strSQl = strSQl & vbCrLf & " ,SeqNo int Null "
            //    strSQl = strSQl & vbCrLf & " ,ItemNo char(20) null"
            //    strSQl = strSQl & vbCrLf & " ,LocationNo Char(6) null"
            //    strSQl = strSQl & vbCrLf & " ,CostUnit Char(5) null"
            //    strSQl = strSQl & vbCrLf & " ,WtAvgCost decimal(18,6) null"
            //    strSQl = strSQl & vbCrLf & " )"
            //    gcn.Execute strSQl
                
            //    While Not rsHdr.EOF
            //        strSQl = "insert into #TmpImInvLoc(OrdNo,SeqNo,ItemNo,LocationNo,WtAvgCost,CostUnit)"
            //        strSQl = strSQl & vbCrLf & " select a.OrdNo,a.SeqNo,b.ItemNo,b.LocationNo,b.WtAvgCost,a.CostUnit"
            //        strSQl = strSQl & vbCrLf & " from iminvProdIssuesDtl a inner join iminvloc b on a.itemno=b.itemno and a.locno=b.locationno inner join iminvmaster c on a.itemno=c.itemno where  a.ordno='" & rsHdr!OrdNo & "'"
            //        gcn.Execute strSQl
                    
            //        strSQl = "select * from #TmpImInvLoc where ordno='" & rsHdr!OrdNo & "'"
            //        Set rsTmpInvLoc = New ADODB.Recordset
            //        rsTmpInvLoc.Open strSQl, gcn, adOpenKeyset, adLockOptimistic
            //        While Not rsTmpInvLoc.EOF
            //'            If mblnUseFIFO Then
            //'                Call getfifocost("RMI", rsTmpInvLoc!OrdNo, rsTmpInvLoc!SeqNo, dblFIFOCost)
            //'                strSql = "select costunit from iminvmaster where itemno='" & rsTmpInvLoc!ItemNo & "' and costunit<>'" & rsTmpInvLoc!CostUnit & "'"
            //'                Set rsItem = gcn.Execute(strSql)
            //'                If rsItem.EOF Then
            //'                    rsTmpInvLoc!WtAvgCost = formatnumeric(dblFIFOCost, "n." & itemscale(gCostScale, rsTmpInvLoc!ItemNo))
            //'                Else
            //'                    Call UnitCnv(rsTmpInvLoc!ItemNo, dblFIFOCost, rsTmpInvLoc!CostUnit, rsItem!CostUnit, rsTmpInvLoc!WtAvgCost)
            //'                    rsTmpInvLoc!WtAvgCost = formatnumeric(rsTmpInvLoc!WtAvgCost, "n." & itemscale(gCostScale, rsTmpInvLoc!ItemNo))
            //'                End If
            //'                rsTmpInvLoc.Update
            //'            Else
            //                If UseActualCost(rsTmpInvLoc!ItemNo) Then
            //                    rsTmpInvLoc!WtAvgCost = GetTaglotActualCost(rsTmpInvLoc!OrdNo, rsTmpInvLoc!SeqNo, rsTmpInvLoc!CostUnit, , 6)
            //                    rsTmpInvLoc.Update
            //                End If
            //'            End If
                        
            //            rsTmpInvLoc.MoveNext
            //        Wend
                    
                    
                    
            //        Set lst = LstPost.ListItems.Add(, , "")
            //        lst.ListSubItems.Add , , rsHdr!OrdNo
            //        lst.Checked = True
            //        strSQl = "select sum(case when isnull(a.cost,0)=0 then (b.WtAvgCost-a.cost)*a.totcost else (b.WtAvgCost-a.cost)*a.totcost/a.cost end )  from iminvProdIssuesDtl a inner join #TmpImInvLoc b on a.itemno=b.itemno and a.locno=b.locationno  and a.Ordno=b.Ordno and a.Seqno=b.Seqno inner join iminvmaster c on a.itemno=c.itemno where c.stocking=1 and a.cost<>b.WtAvgCost and a.ordno='" & rsHdr!OrdNo & "'"
            //        If rsPl.State = adStateOpen Then rsPl.Close
            //        rsPl.Open strSQl, gcn, adOpenForwardOnly, adLockReadOnly
                    
                    
            //        sngPL = Val("" & rsPl.Fields(0))
            //        If sngPL <> 0 Then
            //            lst.ListSubItems.Add , , "Stocking only Cost Changed. "
                        
                        
            //            lst.Tag = "PL"
            //        Else
            //            lst.ListSubItems.Add , , "Cost Not Changed."
            //        End If
            //        rsHdr.MoveNext
            //    Wend
                 
                
            //    Release_RS rsHdr
            //    FillListPost = True
            //    Exit Function
                #endregion ProductionIssues
                return true;
            }
            else if (this.FromModule==FromModuleType.InvCostAdj)
            {
                #region InvCostAdj
                //FillListPost = False
                //blnChangCost = False
                //sngMax = 0
                //sngMin = 0
                
                //InitHead
                
                //strSQl = "select a.AdjNo,a.itemno,a.locationno,a.initonhand,b.OnHand from imadjhdr a "
                //strSQl = strSQl & vbCrLf & " inner join iminvloc b on a.itemno=b.itemno and a.locationno=b.locationno inner join iminvmaster c on a.itemno=c.itemno"
                //strSQl = strSQl & vbCrLf & " where " & strCondition & " order by a.AdjNo"
                //Set rsHdr = New ADODB.Recordset
                //Set rsPl = New ADODB.Recordset
                
                //rsHdr.Open strSQl, gcn, adOpenKeyset, adLockPessimistic
                //If rsHdr.RecordCount = 0 Then
                //    FillListPost = False
                //    Release_RS rsHdr
                //    Exit Function
                //End If
                
                //Dim intQtyScale As Integer
                
                //While Not rsHdr.EOF
                //    Set lst = LstPost.ListItems.Add(, , "")
                //    lst.ListSubItems.Add , , rsHdr!Adjno
                //    lst.Checked = True
                    
                //    intQtyScale = ItemScale(gQtyScale, rsHdr!ItemNo)
                    
                //    If FormatNumeric(rsHdr!initonhand, "n." & intQtyScale) <> FormatNumeric(rsHdr!OnHand, "n." & intQtyScale) Then
                //        lst.ListSubItems.Add , , rsHdr!ItemNo
                //        lst.ListSubItems.Add , , rsHdr!locationNo
                //        lst.ListSubItems.Add , , "Qty OnHand Changed. Are you sure change adjust cost ?"
                //        lst.Tag = "PL"
                //        lst.Checked = False
                //        blnChangCost = True
                //    End If
                //    rsHdr.MoveNext
                //Wend
                 
                
                //Release_RS rsHdr
                //FillListPost = True
                //Exit Function
                #endregion InvCostAdj
                return true;
            }
            else
            {
                #region InvTagTrf
                //FillListPost = False
                //blnChangCost = False
                //sngMax = 0 ' GetmMiscValue("WtAvgCostChangedMax", "RM")
                //sngMin = 0 'GetmMiscValue("WtAvgCostChangedMin", "RM")
                
                //InitHead
                
                //strSQl = "select * from ImInvTagTrfhdr where " & strCondition & " order by ordno"
                //Set rsHdr = New ADODB.Recordset
                //Set rsPl = New ADODB.Recordset
                
                //rsHdr.Open strSQl, gcn, adOpenKeyset, adLockPessimistic
                //If rsHdr.RecordCount = 0 Then
                //    FillListPost = False
                //    Release_RS rsHdr
                //    Exit Function
                //End If
                
                //While Not rsHdr.EOF
                //    Set lst = LstPost.ListItems.Add(, , "")
                //    lst.ListSubItems.Add , , rsHdr!OrdNo
                //    lst.Checked = True
                //    strSQl = "select sum(case when isnull(a.cost,0)=0 then (b.WtAvgCost-a.cost)*a.totcost else (b.WtAvgCost-a.cost)*a.totcost/a.cost end )  from ImInvTagTrfdtl a inner join iminvloc b on a.itemno=b.itemno and a.locno=b.locationno inner join iminvmaster c on a.itemno=c.itemno where c.stocking=1 and a.trftype=0  and a.cost<>b.WtAvgCost and a.ordno='" & rsHdr!OrdNo & "'"
                //    If rsPl.State = adStateOpen Then rsPl.Close
                //    rsPl.Open strSQl, gcn, adOpenForwardOnly, adLockReadOnly
                    
                //    Dim rsCost As ADODB.Recordset
                //    strSQl = "select sum(case when a.trftype=0 then -1 * a.totcost else a.totcost end) as totcost from  ImInvTagTrfdtl a where a.ordno='" & rsHdr!OrdNo & "'"
                //    Set rsCost = gcn.Execute(strSQl)
                    
                //    sngPL = Val("" & rsPl.Fields(0))
                //    If sngPL <> 0 Then
                //        sngPL = Val(FormatNumeric((Val("" & rsCost!totcost) - sngPL), "n.2"))
                //        lst.ListSubItems.Add , , "Stocking only Weighted Average Cost Changed. Total Out Of Balance by " & FormatNumeric(sngPL, "NS.2")
                //        lst.ListSubItems.Add , , sngPL
                //        If sngPL < sngMin Or sngPL > sngMax Then
                //            lst.Checked = False
                //            blnChangCost = True
                //        End If
                //        lst.Tag = "PL"
                //    Else
                //        lst.ListSubItems.Add , , "Weighted Average Cost Not Changed."
                //    End If
                //    rsHdr.MoveNext
                //Wend
                 
                
                //Release_RS rsHdr
                //FillListPost = True
                //Exit Function
                #endregion InvCostAdj
                return true;
            }

            return false;
        }

        private void InitHead()
        {
 	        
            if(this.FromModule==FromModuleType.Reman)
            {
                string[] HeadArray = new string[] {"Post?", "W/O#", "", "PL"};
                int[] WidthArray = new int[] {60, 80, 600,0};
                LstPost.Columns.Clear(); 
                for(int i=0;i<HeadArray.Length;i++)
                {
                    ColumnHeader col = new ColumnHeader();
                    col.Text=HeadArray[i];
                    col.Width=WidthArray[i];
                    LstPost.Columns.Add(col);
                }
            }
            else if(this.FromModule==FromModuleType.BackFlashing)
            {
                string[] HeadArray = new string[] {"Post?", "ORD#", "", "PL"};
                int[] WidthArray = new int[] {60, 80, 600,0};
                for(int i=0;i<HeadArray.Length;i++)
                {
                    ColumnHeader col = new ColumnHeader();
                    col.Text=HeadArray[i];
                    col.Width=WidthArray[i];
                    LstPost.Columns.Add(col);
                }
            }
            else if(this.FromModule==FromModuleType.ProductionIssues)
            {
                string[] HeadArray = new string[] {"Post?", "ORD#", "", ""};
                int[] WidthArray = new int[] {60, 80, 600,0};
                for(int i=0;i<HeadArray.Length;i++)
                {
                    ColumnHeader col = new ColumnHeader();
                    col.Text=HeadArray[i];
                    col.Width=WidthArray[i];
                    LstPost.Columns.Add(col);
                }
            }
            else if(this.FromModule==FromModuleType.InvCostAdj)
            {
                                string[] HeadArray = new string[] {"Post?", "Adj#", "Item#", "Loc#", ""};
                int[] WidthArray = new int[] {60, 80, 120,50,540,0};
                for(int i=0;i<HeadArray.Length;i++)
                {
                    ColumnHeader col = new ColumnHeader();
                    col.Text=HeadArray[i];
                    col.Width=WidthArray[i];
                    LstPost.Columns.Add(col);
                }
            }
            else
            {
                string[] HeadArray = new string[] {"Post?", "Trf#", "", "Balance"};
                int[] WidthArray = new int[] {60, 80, 600,0};
                for(int i=0;i<HeadArray.Length;i++)
                {
                    ColumnHeader col = new ColumnHeader();
                    col.Text=HeadArray[i];
                    col.Width=WidthArray[i];
                    LstPost.Columns.Add(col);
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Condition = GetPostCondition();
            this.Close();
        }

        private string GetPostCondition()
        {
            StringBuilder strConditionBuilderBuilderBuilder=new StringBuilder ("");
            string strOrdNo="",strSql="";
            double dblPL=0;
            if( FromModule==FromModuleType.Reman )
            {
                #region reman
                DBAccess db = new DBAccess(false, true);
                try
                {
                    foreach (ListViewItem lst in LstPost.Items)
                    {
                        if (lst.Checked)
                        {
                            strOrdNo = lst.SubItems[1].Text;
                            strConditionBuilderBuilderBuilder.AppendFormat(" '{0}',", strOrdNo);
                            if (lst.Tag == "PL")
                            {
                                strSql = "update rmwkorderinput set rmwkorderinput.cost=#TmpImInvLoc.WtAvgCost,rmwkorderinput.TotCost=(case when isnull(rmwkorderinput.cost,0)=0 then 0 else (#TmpImInvLoc.WtAvgCost-rmwkorderinput.cost)*rmwkorderinput.totcost/rmwkorderinput.cost end )+rmwkorderinput.totcost from rmwkorderinput  inner join #TmpImInvLoc on rmwkorderinput.itemno=#TmpImInvLoc.itemno and rmwkorderinput.locno=#TmpImInvLoc.locationno and rmwkorderinput.Ordno=#TmpImInvLoc.Ordno and rmwkorderinput.Seqno=#TmpImInvLoc.Seqno inner join iminvmaster on rmwkorderinput.itemno=iminvmaster.itemno where iminvmaster.stocking=1 and rmwkorderinput.cost<>#TmpImInvLoc.WtAvgCost and rmwkorderinput.ordno='" + strOrdNo + "'";
                                db.ExecuteSql(strSql);

                                dblPL = CvtFunc.ToDouble(lst.SubItems[3].Text);
                                strSql = string.Format("update rmwkorderhdr set TotCompCost=TotProdCost-TotTraCost-({0}),profitloss={0} where ordno='{1}'", dblPL, strOrdNo);
                                db.ExecuteSql(strSql);
                            }
                        }
                    }

                    if (strConditionBuilderBuilderBuilder.ToString().TrimEnd() == "")
                    {
                        db.RollbackTranAndClose();
                    }
                    else
                    {
                        db.CommitTranAndCloseCon();
                        return "ordno in (" + strConditionBuilderBuilderBuilder.ToString().Substring(0, strConditionBuilderBuilderBuilder.ToString().Length - 1) + ")";
                    }

                }
                catch (Exception e)
                {
                    db.RollbackTranAndClose();
                    ExceptionManager.ProcessException(e);
                }
                
                #endregion reman
            }
            else if ( FromModule==FromModuleType.BackFlashing  )
            {
                #region BackFlashing
                //GetPost = False
                //strConditionBuilderBuilderBuilder = ""
                //gcn.BeginTrans
                //For i = 1 To LstPost.ListItems.Count
                //    If LstPost.ListItems(i).Checked Then
                //        strConditionBuilderBuilderBuilder = strConditionBuilderBuilderBuilder & " '" & LstPost.ListItems(i).SubItems(1) & "',"
                //        If LstPost.ListItems(i).Tag = "PL" Then
                //            strSQl = "update rmBackFlashinginput   set rmBackFlashinginput.cost=#TmpImInvLoc.WtAvgCost,rmBackFlashinginput.TotCost=(case when isnull(rmBackFlashinginput.cost,0)=0 then 0 else (#TmpImInvLoc.WtAvgCost-rmBackFlashinginput.cost)*rmBackFlashinginput.totcost/rmBackFlashinginput.cost end )+rmBackFlashinginput.totcost from rmBackFlashinginput  inner join #TmpImInvLoc on rmBackFlashinginput.itemno=#TmpImInvLoc.itemno and rmBackFlashinginput.locno=#TmpImInvLoc.locationno and rmBackFlashinginput.Ordno=#TmpImInvLoc.Ordno and rmBackFlashinginput.Seqno=#TmpImInvLoc.Seqno inner join iminvmaster on rmBackFlashinginput.itemno=iminvmaster.itemno where iminvmaster.stocking=1 and rmBackFlashinginput.cost<>#TmpImInvLoc.WtAvgCost and rmBackFlashinginput.ordno='" & LstPost.ListItems(i).SubItems(1) & "'"
                //            gcn.Execute strSQl
                //            strSQl = "update rmBackFlashinghdr set TotCompCost=TotProdCost-TotTraCost-(" & Val(LstPost.ListItems(i).SubItems(3)) & "),profitloss=" & Val(LstPost.ListItems(i).SubItems(3)) & " where ordno='" & LstPost.ListItems(i).SubItems(1) & "'"
                //            gcn.Execute strSQl
                //        End If
                //    End If
                //Next
                
                //If strConditionBuilderBuilderBuilder = "" Then
                //    gcn.RollbackTrans
                //    Exit Function
                //Else
                //    strConditionBuilderBuilderBuilder = "ordno in (" & Left(strConditionBuilderBuilderBuilder, Len(strConditionBuilderBuilderBuilder) - 1) & ")"
                //End If
                //gcn.CommitTrans
                //GetPost = True
                //Exit Function
                #endregion BackFlashing
            }
            else if ( FromModule==FromModuleType.ProductionIssues   )
            {
                #region ProdIssues
                //GetPost = False
                //strConditionBuilderBuilderBuilder = ""
                //gcn.BeginTrans
                //For i = 1 To LstPost.ListItems.Count
                //    If LstPost.ListItems(i).Checked Then
                //        strConditionBuilderBuilderBuilder = strConditionBuilderBuilderBuilder & " '" & LstPost.ListItems(i).SubItems(1) & "',"
                //        If LstPost.ListItems(i).Tag = "PL" Then
                //            strSQl = "update iminvProdIssuesDtl   set iminvProdIssuesDtl.cost=#TmpImInvLoc.WtAvgCost,iminvProdIssuesDtl.TotCost=(case when isnull(iminvProdIssuesDtl.cost,0)=0 then 0 else (#TmpImInvLoc.WtAvgCost-iminvProdIssuesDtl.cost)*iminvProdIssuesDtl.totcost/iminvProdIssuesDtl.cost end )+iminvProdIssuesDtl.totcost from iminvProdIssuesDtl  inner join #TmpImInvLoc on iminvProdIssuesDtl.itemno=#TmpImInvLoc.itemno and iminvProdIssuesDtl.locno=#TmpImInvLoc.locationno and iminvProdIssuesDtl.Ordno=#TmpImInvLoc.Ordno and iminvProdIssuesDtl.Seqno=#TmpImInvLoc.Seqno inner join iminvmaster on iminvProdIssuesDtl.itemno=iminvmaster.itemno where iminvmaster.stocking=1 and iminvProdIssuesDtl.cost<>#TmpImInvLoc.WtAvgCost and iminvProdIssuesDtl.ordno='" & LstPost.ListItems(i).SubItems(1) & "'"
                //            gcn.Execute strSQl
                            
                //        End If
                //    End If
                //Next
                
                //If strConditionBuilderBuilderBuilder = "" Then
                //    gcn.RollbackTrans
                //    Exit Function
                //Else
                //    strConditionBuilderBuilderBuilder = "ordno in (" & Left(strConditionBuilderBuilderBuilder, Len(strConditionBuilderBuilderBuilder) - 1) & ")"
                //End If
                //gcn.CommitTrans
                //GetPost = True
                //Exit Function
                #endregion ProdIssues
            }
            else if ( FromModule==FromModuleType.InvCostAdj   )
            {
                #region inv costadj
                //GetPost = False
                //strConditionBuilderBuilderBuilder = ""
                
                //strSQl = "select a.Recid,a.AdjNo,a.itemno,a.locationno,a.Cost,a.initonhand,a.InvAdjAmt,a.Accruadjamt,b.OnHand,c.CostUnit,c.StockUnit from imadjhdr a "
                //strSQl = strSQl & vbCrLf & " inner join iminvloc b on a.itemno=b.itemno and a.locationno=b.locationno inner join iminvmaster c on a.itemno=c.itemno"
                //strSQl = strSQl & vbCrLf & " where a.IsAdjQty=0 and a.posted=0"
                //strSQl = strSQl & vbCrLf & " order by a.AdjNo"
                //Set rsTable = gcn.Execute(strSQl)
                
                //Dim dblCost As Double, dblQty As Double, dblQtyInCostUnit As Double, dblAdjAmt As Double, dblAccruAdjAmt As Double
                //Dim intQtyScale As Integer, intCostScale As Integer
                
                //gcn.BeginTrans
                
                //For i = 1 To LstPost.ListItems.Count
                //    If LstPost.ListItems(i).Checked Then
                //        If LstPost.ListItems(i).Tag = "PL" Then
                            
                //            rsTable.Filter = "AdjNo='" & LstPost.ListItems(i).SubItems(1) & "'"
                //            If Not rsTable.EOF Then
                //                intQtyScale = ItemScale(gQtyScale, rsTable!ItemNo)
                //                intCostScale = ItemScale(gCostScale, rsTable!ItemNo)
                //                If rsTable!OnHand <> 0 Then
                //                    dblCost = rsTable!Cost
                //                    dblCost = FormatNumeric((dblCost * rsTable!initonhand) / FormatNumeric(rsTable!OnHand, "n." & intQtyScale), "n." & intCostScale)
                //                    dblQty = FormatNumeric(rsTable!OnHand, "n." & intQtyScale)
                                    
                                    
                //                    dblQtyInCostUnit = UnitCnv(rsTable!ItemNo, dblQty, rsTable!StockUnit, rsTable!CostUnit, dblQtyInCostUnit, gcn)
                //                    dblAdjAmt = FormatNumeric(dblQtyInCostUnit * dblCost, "n.2")
                //                    dblAccruAdjAmt = rsTable!Accruadjamt
                //                    dblAdjAmt = FormatNumeric(dblAdjAmt - dblAccruAdjAmt, "n.2")
                //                    strSQl = "Update imadjhdr set Cost=" & dblCost & ",initonhand=" & dblQty & ",InvAdjAmt=" & dblAdjAmt & " where Recid=" & rsTable!RecId
                //                    gcn.Execute strSQl
                //                Else
                //                    strConditionBuilderBuilderBuilder = strConditionBuilderBuilderBuilder & " '" & LstPost.ListItems(i).SubItems(1) & "',"
                //                End If
                //            Else
                //                strConditionBuilderBuilderBuilder = strConditionBuilderBuilderBuilder & " '" & LstPost.ListItems(i).SubItems(1) & "',"
                //            End If
                //        End If
                //    Else
                //        strConditionBuilderBuilderBuilder = strConditionBuilderBuilderBuilder & " '" & LstPost.ListItems(i).SubItems(1) & "',"
                //    End If
                //Next
                
                //If strConditionBuilderBuilderBuilder <> "" Then
                //    strConditionBuilderBuilderBuilder = "AdjNo not in (" & Left(strConditionBuilderBuilderBuilder, Len(strConditionBuilderBuilderBuilder) - 1) & ")"
                //End If
                
                //gcn.CommitTrans
                //GetPost = True
                //Exit Function
                #endregion inv costadj
            }
            else
            {
                #region inv tag trf
                //GetPost = False
                //strConditionBuilderBuilderBuilder = ""
                //gcn.BeginTrans
                //For i = 1 To LstPost.ListItems.Count
                //    If LstPost.ListItems(i).Checked Then
                //        strConditionBuilderBuilderBuilder = strConditionBuilderBuilderBuilder & " '" & LstPost.ListItems(i).SubItems(1) & "',"
                //        If LstPost.ListItems(i).Tag = "PL" Then
                //            strSQl = "update ImInvTagTrfDtl   set ImInvTagTrfDtl.cost=iminvloc.WtAvgCost,ImInvTagTrfDtl.TotCost=(case when isnull(ImInvTagTrfDtl.cost,0)=0 then 0 else (iminvloc.WtAvgCost-ImInvTagTrfDtl.cost)*ImInvTagTrfdtl.totcost/ImInvTagTrfDtl.cost end )+ImInvTagTrfDtl.totcost from ImInvTagTrfDtl  inner join iminvloc on ImInvTagTrfDtl.itemno=iminvloc.itemno and ImInvTagTrfDtl.locno=iminvloc.locationno inner join iminvmaster on ImInvTagTrfDtl.itemno=iminvmaster.itemno where iminvmaster.stocking=1 and ImInvTagTrfDtl.trftype=0  and ImInvTagTrfDtl.cost<>iminvloc.WtAvgCost and ImInvTagTrfDtl.ordno='" & LstPost.ListItems(i).SubItems(1) & "'"
                //            gcn.Execute strSQl
                //        End If
                //    End If
                //Next
                
                //If strConditionBuilderBuilderBuilder = "" Then
                //    gcn.RollbackTrans
                //    Exit Function
                //Else
                //    strConditionBuilderBuilderBuilder = "ordno in (" & Left(strConditionBuilderBuilderBuilder, Len(strConditionBuilderBuilderBuilder) - 1) & ")"
                //End If
                //gcn.CommitTrans
                //GetPost = True
                //Exit Function
                #endregion inv tag trf
            }


            return "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Condition = "";
            this.Close();
        }
    }
}